//
//  main.m
//  OCLearning
//
//  Created by Caldremch on 2018/5/24.
//  Copyright © 2018年 Caldremch. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "Student.h"
#import "Student+NewFunc.h"
#import "Nanny.h"
#import "Person.h"
#import "DateUtils.h"
#import "Worker.h"
#import "PersonOne.h"

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        // NSLog() 是OC中的格式化输出函数，相当于C语言中的 printf() 的升级版，不仅可以用来输出C语言中的数据，还可以输出OC中的数据。
        
        
        //@符号没有实际含义，只是用来作为OC字符串的特有标志。在OC中，字符串前面都要加@，如果不加，就变成了C语言中的字符串，就要使用字符指针或字符数组来定义，
        NSLog(@"Hello, World!");
        
        int a = 100;
        float b = 23.33;
        char *str1 = "object-C";
        NSString *str2 = @"this is object-c string";
        
        //NSString 是OC中的字符串类型，需要使用格式控制符%@来输出。
        NSLog(@"a:%d\nb:%f\nstr1:%s\nstr2:%@\n",a,b,str1,str2);
        
        //alloc函数为对象分配内存空间，init函数会初始化
        Student *stu1 = [[Student alloc] init];
    
        Nanny *nanny = [[Nanny alloc]init];
        
        stu1.delegate = nanny;
        
        
//    respondsToSelector:方法：这个方法是NSObject协议中的方法，功能是判断代理实现方是否已经实现了代理中的某个方法。
//        使用这个方法的好处就是，如果代理实现方没有实现这个代理方法，整个工程不至于崩溃(如果不使用这个方法做判断，一旦你调用了只声明没有实现的方法，程序将会出错。这也就解释了为什么创建协议的时候，默认至少该协议要服从NSObject这个协议了)。
//
//        @selector():专门在调用方法时用到，如果你看到某个方法的参数是SEL类型时，就说明这需要传进去一个方法，书写语法为：@selector(方法)（这里的方法是简写的形式，就是不显示参数名）。
        
        if ([stu1.delegate respondsToSelector:@selector(studentDelegateFunc)]){
            [stu1.delegate studentDelegateFunc];
        }else{
            NSLog(@"不会吧");
        }
        

        
        stu1.name = @"小明";
        stu1.age = 13;
        stu1.score = 92.23;
        
    
        //单例模式
        
        //调用类函数
        [stu1 display];
        
        [Student params:@"fucking" andAge:40];
        
        [Student paramsTwo:@"caldremch" :23];
        
        [stu1 eat];
        
        Person *zs = [[Person alloc]init];
         Person *ls = [[Person alloc]init];
        
        [zs displayWithDriveHours:5];
        [ls displayWithDriveHours:10];
        
        Car *car = [Car car];
        
        NSLog(@"The allHours are %d\n", car.driveHours);
        
        //字符串的创建
        int count = 5;
        NSString *str3 =[NSString stringWithFormat:@"The count is: %d", count];
        NSLog(@"%@", str3);
        NSLog(@"%ld",[str3 length]);
        NSLog(@"%ld", str3.length);
        
        //字符串比较  等于比较
        NSString * str4 = @"abc";
        NSString * str5 = @"ABC";
        NSString * str6 = @"abc";
        int equal = [str4 isEqualToString:str5];
        int equal1 = [str4 isEqualToString:str6];
        NSLog(@"%d", equal);
        NSLog(@"%d", equal1);
        
        NSString *str7 = @"init like this";
        str7 = @"init second tiems";
        NSLog(@"%@", str7);
        
        //字符串包含
        int i = [str4 containsString:str5];
        
        NSLog(@"containsString: %d", i);
        
        
//        NSMutableString
        
        NSMutableString *str8 =  [NSMutableString stringWithString:@"c.net"];
        [str8 insertString:@"bigbang" atIndex:2];
        NSLog(@"%@", str8);
        
        //以下未错误代码  注意：可变字符串不能直接赋值。必须给其分配空间。
        /*NSMutableString *str9 = @"fucking string";
        [str9 appendFormat:@"fuck you"];
        NSLog(@"%@", str9);*/
        
        //字符串的截取
        NSMutableString * str10 = [NSMutableString stringWithString:@"c.biancheng.net"];
        NSRange range;
        range.location = 2;
        range.length = 9;
        [str10 deleteCharactersInRange:range];
        NSLog(@"%@", str10);
        
        
        
        //NSArray
        
        NSArray *array =[ NSArray arrayWithObjects:@"0",@"1",@"2",nil];
        NSLog(@"array are: %@", array);
        
        NSArray *array1 = [[NSArray alloc] initWithObjects:@"00",@"11",nil];
        NSLog(@"array1 are: %@", array1);
        
        //NSArray 长度
        
        NSInteger countA = [array count];
        NSLog(@"%ld", countA);//也可以用array1.count
        NSLog(@"%ld", array.count);//也可以用array1.count
        
        //根据索引获取值
        NSString *strIndex0 = [array objectAtIndex:0];
        NSLog(@"%@", strIndex0);
        
        
//        判断一个对象是否包含在一个数组中
//        - (BOOL)containsObject:(ObjectType)anObject;
        NSLog(@"%d", [array containsObject:@"1"]);
        
        
        //增加对象
        NSMutableArray *mutableArray = [NSMutableArray arrayWithObjects:@"0",@"1",@"2",nil];
        [mutableArray addObject:@"3"];
        NSLog(@"%@", mutableArray);
        
        //一次加多个对象
        [mutableArray addObjectsFromArray:array1];
        
         NSLog(@"%@", mutableArray);
        
        

        
//            [array replaceObjectAtIndex:2 withObject:@"7"];
        
        
        //NSDictionary 字典 键和值都可以试数组 NSArray 对象
        
        NSDictionary *dic1 = nil;
        dic1= @{@"key":@"value",@"key2": @"value2"};
//        NSLog(@"value=%@", [dic1 objectForKey:@"key1"]);
        NSLog(@"dic1 has: %@", dic1);
        NSLog(@"dic1 has: %@", dic1.allKeys);
        NSLog(@"dic1 has: %@", dic1.allValues);
        
        
        //获取时间  NSDate
        NSDate *d=[NSDate date];
        long time=(long)[d timeIntervalSince1970];
        NSLog(@"%ld",time);
        NSDateFormatter * formatter = [[NSDateFormatter alloc]init];
        [formatter setDateFormat:@"YYYY-MM-dd"];
        NSString *dateStr = [formatter stringFromDate:d];
        NSLog(@"%@",dateStr);
        
        NSLog(@" 时间戳转为 NSDate: %@ ", [DateUtils changeTimeStamp2Time:[NSString stringWithFormat:@"%ld", time]]);
        NSLog(@" 给定时间与当前时间的相差 NSDate: %ld ", [DateUtils timeDifferent:d]);
//        NSLog(@" 给定时间与当前时间的相差 NSDate: %ld ", [DateUtils nsDate2String:d]);
        
        
        //NSData
        NSString * str20 = @"http://c.xxxx.xxxxx";
        NSData * da = [str20 dataUsingEncoding:NSUTF8StringEncoding];
        NSLog(@"%@",da);
        NSString * strCopy = [[NSString alloc] initWithData:da encoding:NSUTF8StringEncoding];
        NSLog(@"%@", strCopy);
        
        
        
        //NSNumber
        //Foundation框架中NSArray、NSDictionary的学习，对于OC语言中的存储类型所存储的都是对象，C语言中的int等基本数据类型无法存到数组、字典中。
        int number = 5;
        NSNumber * intNum = [[NSNumber alloc] initWithInt:number];
        
        //将对象5存到数组
        NSArray * arrayNum = [NSArray arrayWithObjects:intNum, nil];
        NSLog(@"the array is : %@", arrayNum);
        
        NSNumber* getNum = [arrayNum objectAtIndex:0];
        int getNumber = [getNum intValue];
        NSLog(@"the number is %d", getNumber);
        
        
        //NSValue
        NSRange range2;
        range2.length = 2;
        range2.location = 1;
        
        //将 range 转成对象
        NSValue *value = [NSValue valueWithRange:range2];
        
        //将 NSRange 对象转换成 NSRange 变量
        NSRange theRange = [value rangeValue];
        
        //输出结构体的数据
        NSLog(@"%lu, %lu",(unsigned long) theRange.length, theRange.location);
        
        
        Worker *woker = [[Worker alloc]init];
         Worker *woker1 = [[Worker alloc]init];
        [[NSNotificationCenter defaultCenter]postNotificationName:@"canMake" object:nil];
        //记得立马移除
        [[NSNotificationCenter defaultCenter]removeObserver:woker name:@"canMake" object:nil];
        [[NSNotificationCenter defaultCenter]removeObserver:woker1 name:@"canMake" object:nil];
        
        
        
        //谓词
        NSArray * arrayPerson =  [NSArray arrayWithObjects:[PersonOne personWithName:@"zs" age:14],
                                  [PersonOne personWithName:@"zs" age:14],
                                  [PersonOne personWithName:@"zs1" age:15],
                                  [PersonOne personWithName:@"zs2" age:16],
                                  [PersonOne personWithName:@"zs2" age:17],
                                  [PersonOne personWithName:@"zs3" age:18],
                                  [PersonOne personWithName:@"zs4" age:19],
                                  nil
                                  ];
        
        NSPredicate * predicate = [NSPredicate predicateWithFormat:@"age>17"];
        NSArray * myArray = [arrayPerson filteredArrayUsingPredicate:predicate];
        for (PersonOne * person in myArray){
            NSLog(@"age>17: %@", person.name);
        }
        
        //Block 声明的时候（）或（void）但是不能连括号都不写。
        void (^helloworld)(void);
        
        helloworld=^(void){
            NSLog(@"hello world");
        };
        
        helloworld();
        
//        如果试图在块内更改局部变量的值，程序会报错，解决的方案是在声明局部变量时添加__block关键字（注意是两个“_”）：
        __block int ib = 10;
        
        //实现的时候:（void）或（）或干脆省略都可以。
        void(^block)(void)=^(void){
            ib++;
            NSLog(@"the i is %d", ib);
        };
        
        
        block();
        
        
        NSArray *arrayBlock = [NSArray arrayWithObjects:@"4", @"1",@"2",@"3",@"5",nil];
        
        arrayBlock = [arrayBlock sortedArrayUsingComparator:^NSComparisonResult(id  _Nonnull obj1, id  _Nonnull obj2) {
            int a1 = [obj1 intValue];
            int a2 = [obj2 intValue];
            
            return a1>a2?-1:1;
        }];
        
        
        PersonOne * per = [[PersonOne alloc] init];
        [per exercise:^(NSString *name, int age) {
            NSLog(@"%@, %d", name, age);
        }];
        
        //数据存储
        NSString *path = @"/Users/caldremch/Desktop/a.txt";
        NSString *string = @"c.bing.sss";
        NSError * error = nil;
        
        [string writeToFile:path atomically:YES encoding:NSUTF8StringEncoding error:&error];
        
        
        NSString *fileStr = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:&error];
        
        NSLog(@"%@", fileStr);
        NSLog(@"%@", error);
        
        //归档对象
        PersonOne *person2 = [[PersonOne alloc] init];
        person2.name = @"Caldremch";

        person2.age = 23;
        
        [NSKeyedArchiver archiveRootObject:person2 toFile:path];
        
        //解档一个对象
        PersonOne *welPer = [NSKeyedUnarchiver unarchiveObjectWithFile:path];
        NSLog(@"%@", welPer.name);
        
        
        //用户设置, 默认, 运行程序会自动创建一个 plist 来存储数据
        if ([[[NSUserDefaults standardUserDefaults]objectForKey:@"logined"] isEqualToString:@"OK"]) {
            NSLog(@"你登录过了");
        }else{
            NSLog(@"你第一次登录");
            NSUserDefaults * userDefaults = [NSUserDefaults standardUserDefaults];
            [userDefaults setObject:@"OK" forKey:@"logined"];
            
        }
        
        NSString * path1=[NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) lastObject];
        NSLog(@"%@",path1);
        
    }
    return 0;
}
